package com.example.server;

import com.example.po.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author bigFish
 * @Title: post请求
 */
@RestController
@RequestMapping("/post")
public class PostMethod {

    private static Cookie cookie;

    @ApiOperation("登录返回cookie")
    @PostMapping("/cookies")
    public String login(HttpServletResponse response,
                        @RequestParam(value = "username", required = true) String username,
                        @RequestParam(value = "password", required = true) String password) {
        if (username.equals("admin") && password.equals("123")) {
            cookie = new Cookie("user", "admin");
            response.addCookie(cookie);
            return "登录成功";
        }
        return "用户名或密码错误";
    }

    @ApiOperation("获取用户列表")
    @PostMapping("/getList")
    public String getUserList(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie:cookies){
            if (cookie.getName().equals("admin")&&cookie.getValue().equals("123")){
                User user = new User();
                user.setName("张三");
                user.setAge(12);
                user.setSex("男");
                return user.toString();
            }
        }
        return "请携带cookie进行访问";
    }
}
